﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using aerowan.pilot_client.Network;
using System.Windows;
using aerowan.pilot_client.Network.Packets;
using System.Threading;
using aerowan.pilot_client.Simulator;
using aerowan.common;

namespace aerowan.pilot_client
{
    public class ApplicationContext : INotifyPropertyChanged
    {
        public static ApplicationContext Instance { get; private set; }
        static ApplicationContext()
        {
            Instance = new ApplicationContext();
        }

        #region NotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        bool? _connected = false;
        public bool? Connected
        {
            get { return _connected; }
            set
            {
                _connected = value;
                NotifyPropertyChanged("Connected");
            }
        }

        public int AuthAccountID { get; set; }
        public string AuthPassword { get; set; }
        public string AuthCallsign { get; set; }

        double LastLatitude = 0.0D;
        double LastLongitude = 0.0D;

        ApplicationContext()
        {
            PropertyChanged += new PropertyChangedEventHandler(ApplicationContext_PropertyChanged);
        }

        Thread _positionRefreshThread;

        void ApplicationContext_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Connected")
            {
                if (Connected == true)
                {
                    AirplaneDataLoop();
                }
            }
        }

        void AirplaneDataLoop()
        {
            if (Connected == false) return;
            FlightSimulator.Instance.ReadLong(Offsets.LATITUDE_FS, latitudeValue =>
                {
                    double latitude = Utility.LatitudeFsToDeg(latitudeValue);
                    FlightSimulator.Instance.ReadLong(Offsets.LONGITUDE_FS, longitudeValue =>
                        {
                            double longitude = Utility.LongitudeFsToDef(longitudeValue);
                            if (LastLatitude != latitude || LastLongitude != longitude)
                            {
                                LastLatitude = latitude;
                                LastLongitude = longitude;
                                ServerLayer.Instance.SendPacket(new CSPkt_GPSPosition() { Latitude = latitude, Longitude = longitude });
                            }
                            Thread.Sleep(1500);
                            AirplaneDataLoop();
                        });
                });
        }

        public void Connect()
        {
            if (!ServerLayer.Instance.Open())
            {
                MessageBox.Show("Cannot connect to server.");
                return;
            }
            Connected = null;
            ServerLayer.Instance.SendPacket(new CSPkt_Login() { AccountID = AuthAccountID, Password = Utility.PasswordHash(AuthPassword) });
            // Now wait for reply, somewhere else ... ^^
        }

        public void Disconnect()
        {
            ServerLayer.Instance.Close();
            Connected = false;
        }
    }
}
